package com.jy.api.service.impl;

import com.jy.api.dao.StoreDao;
import com.jy.api.dao.WebRechargeDao;
import com.jy.api.domain.PaymentStatus;
import com.jy.api.domain.Store;
import com.jy.api.domain.WebRecharge;
import com.jy.api.enums.StoreLevel;
import com.jy.api.pay.result.PayResult;
import com.jy.api.service.PayService;
import com.jy.api.service.WebRechargeService;
import com.jy.api.util.MyDateUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * Created by lihu on 2017/10/21.
 */

@Service
public class WebRechargeServiceImpl implements WebRechargeService {

    @Autowired
    PayService payService;

    @Autowired
    StoreDao storeDao;

    @Autowired
    WebRechargeDao webRechargeDao;

    @Override
    @Transactional
    public boolean queryWebRecharge(String webId) throws Exception {
        boolean status = true;
        WebRecharge webRecharge = webRechargeDao.findOne(webId);
        if (webRecharge != null) {
            //如果创建时间超过10分钟直接丢弃
            if (new Date().getTime() - webRecharge.getUpdateTime().getTime() > 1000 * 60 * 10) {
                status = true;
            }
            if (webRecharge.getStatus() == PaymentStatus.NOTSURE) {
                Store store = storeDao.findOne(webRecharge.getStoreId());
                if (store != null) {
                    PayResult queryResult = payService.webQuery(webRecharge.getId());
                    if (StringUtils.equals("0", queryResult.getStatus()) &&
                            StringUtils.equals("0", queryResult.getResultCode()) &&
                            StringUtils.equals("SUCCESS", queryResult.getTradeState())) {
                        webRecharge.setStatus(PaymentStatus.SUCCESS);
                        webRecharge.setAmount(queryResult.getTotalFee());

                        webRechargeDao.save(webRecharge);
                    } else {
                        if (!queryResult.accepted()) {
                            webRecharge.setStatus(PaymentStatus.FAIL);
                            webRechargeDao.save(webRecharge);
                        } else {
                            status = false;
                        }
                    }
                }
            }
        }

        return status;
    }

    @Override
    @Transactional
    public void updateWebRecharge(PayResult payResult) {
        WebRecharge webRecharge = webRechargeDao.findOne(payResult.getOutTradeNo());
        if (webRecharge != null) {
            if (webRecharge.getAmount() != payResult.getTotalFee()) {
                webRecharge.setAmount(payResult.getTotalFee());
            }
            Store store = storeDao.findOne(webRecharge.getStoreId());
            if (webRecharge.getAmount() == 120000) {
                store.setLevel(StoreLevel.TWO);
            } else {
                store.setLevel(StoreLevel.THREE);
            }
            //修改店铺过期时间
            store.setExpireTime(MyDateUtils.getDateDiffYear(store.getExpireTime() != null ?
                    store.getExpireTime() : new Date(), 1));
            storeDao.save(store);
            webRecharge.setStatus(PaymentStatus.SUCCESS);
        }
    }
}
